package tripleo.framework.svr.storage2;

import java.util.*;

import org.apache.commons.logging.*;

import tripleo.framework.fs.*;
import tripleo.fs.File;
import tripleo.histore.j3.HiStore_J3;

// Referenced classes of package tripleo.framework.svr.storage:
//			CreateRequest, FileReader, ReadFileRequest, PartialStreamR, 
//			Channel, ChannelResult, DeleteFileRequest

public class ss2_handler {

	private String _ss;
	public ss2_handler(String s) {
		dh = FileSystem.openUnix(s);
		_ss=s;
		//
	}

	

	
	
	transient private static Log log = LogFactory.getLog(HiStore_J3.class);

	

	

	public void create_file_req(CreateRequest Q, Channel CH) {
//		try {
//
//			FileDesc fd;
//			String NC;
//			do {
//				NC = alloc_temp();
//				fd = dh.describe(NC);
//			} while (fd.exists());
//			//
//			final int mode = Modes.FailIfExist | Modes.CreateAlways;
//			final WriteHandle fh = fd.openWrite(mode);
//			final File rd = Q.RD();
//			final long S = 1000;//rd.available();
////			while (rd.available() > 0L)
////				fh.write(rd.read(0x4000000)); // 64K
//			fh.close();
//			String R = _finalize(NC, S);
//			CH.string_success(R);
//		} catch (FileOpFailure e) {
//			CH.fileop_failure(e);
//		}

		CH.default_success();

	}

	public void delete_file_req(DeleteFileRequest Q, Channel CH) {
		try {
			String translated = _translate(Q.CAP());
			FileDesc fileDesc = dh.describe(translated);
			fileDesc.unlink();
			CH.default_success();
		} catch (FileOpFailure e) {
			CH.fileop_failure(e);
		}
	}

	private String _translate(String string) {
		// TODO Auto-generated method stub
		return string;
	}

	public void read_file_req(ReadFileRequest Q, Channel CH) {
		try {
			String translated = _translate(Q.CAP());
			FileDesc fileDesc = dh.describe(translated);
			ReadHandle f = fileDesc.openRead();
			f.seek(Q.startIndex());
			CH.fileread_sucess(null);//new PartialStreamR(f, f.available()));
			f.close();
		} catch (FileOpFailure e) {
			CH.fileop_failure(e);
		}
	}

	private IDirHandler dh;
	static int _tmp = 1;
	private final Random mRandom = new Random();

}
